home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-12-12 | 2.3 KB | 88 lines | [TEXT/CWIE] |
- package blueworld.lasso;
-
- import java.io.*;
-
- public class UTF8Coder
- {
- private UTF8Coder() { }
-
- // This method fixes a bug in DataInputStream.readUTF()
- // where a break is missing from the last case in the switch statement
- public static String decode(String input) throws IOException
- {
- DataInputStream inStream = new DataInputStream(new StringBufferInputStream(input));
- int utflen = input.length();
- char str[] = new char[utflen];
- int count = 0;
- int strlen = 0;
- while (count < utflen)
- {
- int c = inStream.readUnsignedByte();
- int char2, char3;
- switch (c >> 4)
- {
- case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
- // 0xxxxxxx
- count++;
- str[strlen++] = (char)c;
- break;
- case 12: case 13:
- // 110x xxxx 10xx xxxx
- count += 2;
- if (count > utflen)
- throw new UTFDataFormatException();
- char2 = inStream.readUnsignedByte();
- if ((char2 & 0xC0) != 0x80)
- throw new UTFDataFormatException();
- str[strlen++] = (char)(((c & 0x1F) << 6) | (char2 & 0x3F));
- break;
- case 14:
- // 1110 xxxx 10xx xxxx 10xx xxxx
- count += 3;
- if (count > utflen)
- throw new UTFDataFormatException();
- char2 = inStream.readUnsignedByte();
- char3 = inStream.readUnsignedByte();
- if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80))
- throw new UTFDataFormatException();
- str[strlen++] = (char)(((c & 0x0F) << 12) |
- ((char2 & 0x3F) << 6) |
- ((char3 & 0x3F) << 0));
- break; // BUG FIX!
- default:
- // 10xx xxxx, 1111 xxxx
- throw new UTFDataFormatException();
- }
- }
- return new String(str, 0, strlen);
- }
-
- public static String encode(String input) throws IOException
- {
- int strlen = input.length();
-
- ByteArrayOutputStream result = new ByteArrayOutputStream(strlen);
-
- for (int i = 0 ; i < strlen ; i++)
- {
- int c = input.charAt(i);
- if ((c >= 0x0001) && (c <= 0x007F))
- {
- result.write(c);
- }
- else if (c > 0x07FF)
- {
- result.write(0xE0 | ((c >> 12) & 0x0F));
- result.write(0x80 | ((c >> 6) & 0x3F));
- result.write(0x80 | ((c >> 0) & 0x3F));
- }
- else
- {
- result.write(0xC0 | ((c >> 6) & 0x1F));
- result.write(0x80 | ((c >> 0) & 0x3F));
- }
- }
- return result.toString(0);
- }
- }
-